package com.caishi.lkx.user.api;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.caishi.lkx.common.AdminBaseApi;
import com.caishi.lkx.common.role.Role;
import com.caishi.lkx.common.role.Roles;
import com.caishi.lkx.user.ienum.type.HotWordType;
import com.caishi.lkx.user.model.HotWordModel;
import com.caishi.lkx.user.service.IHotWordService;
import com.zzw.common.Wrappers;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Tag(name ="热词相关接口")
@RestController
@RequestMapping("/user/sys/hot-word")
public class HotWordApi extends AdminBaseApi<IHotWordService, HotWordModel, String> {

    @Resource
    private IHotWordService hotWordService;

    @Override
    public IHotWordService loadService() {
        return hotWordService;
    }

    @Override
    protected LambdaQueryWrapper<HotWordModel> baseWrapper(HotWordModel model, JSONObject otherParam) {
        String startTime = otherParam.getStr("startTime");
        String endTime=otherParam.getStr("endTime");
        return super.baseWrapper(model.clearEmptyStringField("name"), otherParam)
                .like(StrUtil.isNotBlank(model.getName()),HotWordModel::getName,model.getName())
                .ge(StrUtil.isNotBlank(startTime),HotWordModel::getCreatedTime,startTime)
                .le(StrUtil.isNotBlank(endTime),HotWordModel::getCreatedTime,endTime);
    }

    @Override
    protected List<HotWordModel> listCover(List<HotWordModel> result, JSONObject otherParam) {
        return result;
    }


    @GetMapping("/client/list")
    @Operation(summary = "前台热词搜索")
    @Parameters({
            @Parameter(name = "size",description = "搜索长度"),
            @Parameter(name = "name",description = "关键词"),
            @Parameter(name = "type",description = "热词类型"),
    })
    @Roles(Role.none)
    public List<HotWordModel> webList(@RequestParam(defaultValue = "10") Integer size, @RequestParam(required = false) String name, @RequestParam(defaultValue = "question") HotWordType type) {
        if (size > 1000) {
            size = 1000;
        }
        return loadService().selectList(Wrappers.<HotWordModel>lambdaQuery().eq(HotWordModel::getType,type).eq(HotWordModel::getEnable,true).like(StrUtil.isNotBlank(name),HotWordModel::getName,name).orderByDesc(HotWordModel::getSort).last("limit "+size));
    }

    @DeleteMapping("/deleted-list")
    public Boolean delete(@RequestParam List<String> ids) {
        return loadService().delete(ids);
    }
}
